package faircat.android.exphone;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;

public class ExCallLog{
	public static final String KEY_ID = "_id";
	public static final String KEY_DISPLAY_NAME = "name";
	public static final String KEY_NUMBER = "number";
	public static final String KEY_DATE = "date";
	public static final String KEY_TYPE = "type";
	public static final String KEY_DURATION = "duration";
	//public static final String KEY_SLOT = "slot";
	//public static final String KEY_ID2 = "id2";
	private static final String TAG = "ExCallLog";
	private static final String DATABASE_NAME = "exphone1";
	private static final String DATABASE_TABLE = "calllog";
	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_CREATE =
			"create table "+DATABASE_TABLE+" ("
			+ KEY_ID+" integer primary key autoincrement, "
			+ KEY_DISPLAY_NAME + " text,"
			+ KEY_NUMBER + " text not null,"
			+ KEY_DATE+" datetime not null,"
			+ KEY_TYPE+" integer not null,"
			+ KEY_DURATION+" text not null"
			+ ");";
	
	public static final int INCOMING_TYPE = 1;   
	public static final int OUTGOING_TYPE = 2;   
	public static final int MISSED_TYPE = 3; 
	
	private final Context context;

	DatabaseHelper mDBHelper;
	SQLiteDatabase mDB;
	
	ExCallLog(Context ctx){
		context = ctx;
		mDBHelper = new DatabaseHelper(context);
		mDB = mDBHelper.getWritableDatabase();
	}
	
	public void close(){
		mDBHelper.close();
	}
	
	public long addCall(String name, String number, long date, int type, long duration){
		ContentValues vals = new ContentValues();
		vals.put(KEY_DISPLAY_NAME, name);
		vals.put(KEY_NUMBER, number);
		vals.put(KEY_DATE, date);
		vals.put(KEY_TYPE, type);
		vals.put(KEY_DURATION, duration);
		//vals.put(KEY_SLOT, slot);
		//vals.put(KEY_ID2, id2);
		return mDB.insert(DATABASE_TABLE, null, vals);
	}
	
	public void deleteCall(long id){
		mDB.delete(DATABASE_TABLE, KEY_ID+'='+id, null);
	}
	
	public void update(long id, int type, long duration){
		ContentValues vals = new ContentValues();
		vals.put(KEY_TYPE, type);
		vals.put(KEY_DURATION, duration);
		mDB.update(DATABASE_TABLE, vals, KEY_ID+'='+id, null);
	}
	
	public void updateType(long id, int type){
		ContentValues vals = new ContentValues();
		vals.put(KEY_TYPE, type);
		mDB.update(DATABASE_TABLE, vals, KEY_ID+'='+id, null);
	}
	
	public void updateDuration(long id, long duration){
		ContentValues vals = new ContentValues();
		vals.put(KEY_DURATION, duration);
		mDB.update(DATABASE_TABLE, vals, KEY_ID+'='+id, null);
	}
	
	public Cursor queryAll(){
		Cursor cursor = mDB.query(DATABASE_TABLE, 
				new String[]{KEY_ID, KEY_DISPLAY_NAME, KEY_NUMBER, KEY_DATE, KEY_TYPE, KEY_DURATION}, 
				null, null, null, null, KEY_DATE+" desc");
		return cursor;
	}
	
	public void deleteAll(){
		mDB.delete(DATABASE_TABLE, null, null);
	}
	
	public Cursor queryByPhone(String number){
		Cursor cursor = mDB.query(DATABASE_TABLE, 
				new String[]{KEY_ID, KEY_DISPLAY_NAME, KEY_NUMBER, KEY_DATE, KEY_TYPE, KEY_DURATION, "count(*) as times"}, 
				KEY_NUMBER+" like '%"+number+"%'", null, KEY_NUMBER, null, KEY_DATE+" desc");
		return cursor;
	}
	/*
	public void addSlot1(){
		String[] projection;
		String selection = null;
		projection = new String[]{
			CallLog.Calls._ID,
			CallLog.Calls.NUMBER,
			CallLog.Calls.DATE,			
			CallLog.Calls.TYPE,
			CallLog.Calls.DURATION,
		};

		if( cursor.moveToFirst()){
			int idx_id = cursor.getColumnIndex(CallLog.Calls._ID);
			int idx_number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
			int idx_date = cursor.getColumnIndex(CallLog.Calls.DATE);
			int idx_type = cursor.getColumnIndex(CallLog.Calls.TYPE);
			int idx_dur = cursor.getColumnIndex(CallLog.Calls.DURATION);
			while(true){
				addCall(cursor.getString(idx_number),
						cursor.getLong(idx_date),
						cursor.getInt(idx_type),
						cursor.getInt(idx_dur),
						0,
						cursor.getInt(idx_id)
						);
				if( !cursor.moveToNext() ){
					break;
				}
			}
			if( !cursor.isClosed() ){
				cursor.close();
			}
		}
	}
	
	public void removeSlot1(){
		mDB.delete(DATABASE_TABLE, KEY_SLOT+"=0", null);
	}
	*/
	class DatabaseHelper extends SQLiteOpenHelper{
		public DatabaseHelper(Context ctx){
			super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		public void onCreate(SQLiteDatabase db){
			db.execSQL(DATABASE_CREATE);
		}
		
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer){
			if( newVer > oldVer ){
				db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
				onCreate(db);
			}
		}
	}
}
